package com.npjscg.medicalpro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.entity.Bcheck;
import com.npjscg.medicalpro.entity.Business;
import com.npjscg.medicalpro.entity.Check;
import com.npjscg.medicalpro.entity.Notification;
import com.npjscg.medicalpro.exception.ServiceException;
import com.npjscg.medicalpro.mapper.BcheckMapper;
import com.npjscg.medicalpro.mapper.BusinessMapper;
import com.npjscg.medicalpro.mapper.NotificationMapper;
import com.npjscg.medicalpro.service.IBcheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author raote
 * @since 2022-07-09
 */
@Service
public class BcheckServiceImpl extends ServiceImpl<BcheckMapper, Bcheck> implements IBcheckService {
    @Autowired
    private Bcheck bcheck;

    @Autowired
    private Notification notification;


    @Resource
    private BusinessMapper businessMapper;

    @Resource BcheckMapper bcheckMapper;

    @Resource
    private NotificationMapper notificationMapper;




    public void addCheck(Business business){
        bcheck.setId(null);
        bcheck.setBid(business.getId());
        bcheck.setMsg("用户已填写,待审核");
        bcheck.setStatus(0);
        save(bcheck);
    }

    @Override
    public void changeStatus(Integer bid, int currentStatus, int adminid) {
        Business byId = businessMapper.selectById(bid);
        UpdateWrapper<Bcheck> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("bid", bid);
        LocalDateTime dateTime = LocalDateTime.now();

        if (byId == null)
            throw new ServiceException(Constants.CODE_600 , "数据库中没有此记录！");
        else {
            if (currentStatus == 1){
                //未通过fdf
                updateWrapper.set("status",currentStatus);
                updateWrapper.set("msg","报表信息有误，未通过！");
                updateWrapper.set("adminid",adminid);
                updateWrapper.set("checkdate",dateTime);
                update(updateWrapper);
                //发送给用户通知
                notification.setId(null);
                notification.setUid(byId.getUid());
                notification.setMsg("您的事务《"+byId.getTitle()+"》未通过审核，请查看！");
                notification.setIsCheck(0);
                notificationMapper.insert(notification);

            } else if (currentStatus == 2) {
                //通过
                updateWrapper.set("status",currentStatus);
                updateWrapper.set("msg","上报成功，通过！");
                updateWrapper.set("adminid",adminid);
                updateWrapper.set("checkdate",dateTime);
                update(updateWrapper);
                //发给用户通过通知
                notification.setId(null);
                notification.setUid(byId.getUid());
                notification.setMsg("您的事务《"+byId.getTitle()+"》已通过审核！");
                notification.setIsCheck(0);
                notificationMapper.insert(notification);

            }
            else{
                throw new ServiceException(Constants.CODE_600, "不能设置成未审核，业务出错");
            }
        }
    }

    @Override
    public void changeMsg(Integer bid, String msg) {
        QueryWrapper<Bcheck> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("bid",bid);
        Bcheck one = getOne(queryWrapper);
        if (one == null)
            throw new ServiceException(Constants.CODE_600, "数据库中没有此记录！");
        else {
            UpdateWrapper<Bcheck> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("bid", bid);
            updateWrapper.set("msg", msg);
            update(updateWrapper);
        }
    }
}
